#include <SypX/SypX.h>

#ifdef SYPX_MEMCHECK
#include "mmgr.h"
#endif

#include "MenuScene.h"
#include "MoreScene.h"
#include "SheepDefenceApp.h"

namespace SheepDefence
{
	MoreScene::MoreScene(SypX::Font* f)
	{
		font = f;
	}

	MoreScene::~MoreScene(void)
	{
		delete bg;
		
		delete back;
		
		engine->getAudioEngine()->deleteSoundBuffer(snd);
		
	}

	void MoreScene::init()
	{
		
		engine = SypX::Engine::getSingleton();
		render = engine->getRenderer();
		canvas = engine->getCanvas();	
		if(!font)
		{
			font = engine->loadFontFromFile("Fonts/sheepFont.ttf", 24, 3);
		}

		bgMesh = new SypX::Mesh();
		bgMesh->initAsQuad(320.0f,480.0f);
		
		bgTex = SypX::Texture::loadFromFile("Textures/bgMore.png");
		
		bg = new SypX::MeshInstance();
		bg->useMesh(bgMesh);
		bg->useTexture(bgTex);

		bg->setPosition(SypX::Vec2(160.0f, 240.0f));		

		render->setClearScreenColor(SypX::Vec4(0.0f, 0.0f,0.0f,1.0f));		
		
		snd = engine->getAudioEngine()->loadFromWAVFile("Sounds/Sega.wav");
		//engine->getAudioEngine()->playSound(snd, SypX::Vec3()) ;

		backTex = SypX::Texture::loadFromFile("Textures/btnBack.png");
		back = new SypX::Button(160.0f, 80.0f, 80.0f, 30.0f, 0.9f, backTex);
		back->onDownCallBack = SYPXBIND(&MoreScene::goBack, this, SYPX_ARG1, SYPX_ARG2);
	}

	void MoreScene::handleInput()
	{
		SypX::Event e = canvas->getEvent();
		back->processInput(e);
	}

	void MoreScene::run()
	{
		render->clearScreen();		
		render->setActiveColor(SypX::Vec4(1.0f,1.0f,1.0f,1.0f));
		render->drawMesh(bg);
		handleInput();
		//totalTime += canvas->getElapsedTime();		
		//if(totalTime >= 6.0f)
		//{
		//	changeScene(new MenuScene(font));			
		//}

		back->draw();
		
		canvas->swapBuffers();
		//changeScene(new GameScene(font));
	}

	void MoreScene::goBack(SypX::GuiComponent* g, SypX::Event& e)
	{
		changeScene(new MenuScene(font));
	}
}
